<?php

namespace app\store\controller\goods;


use app\store\controller\BaseController;

use app\store\model\GoodsTag;
use app\store\service\GoodsTagService;
use think\exception\ValidateException;
use think\facade\View;

class TagController extends BaseController
{
    public function index()
    {   
        $keyword = $this->request->param('keyword','');

        $goodsTagService = new GoodsTagService();
        $list = $goodsTagService->getGoodsTagList($keyword);

        return View::fetch('',['list' => $list]);
    }
    
    public function create()
    {
        View::engine()->layout(false);
        return View::fetch();
    }

    public function edit()
    {   
        $id = $this->request->get('id/d');

        $goodsTagService = new GoodsTagService();
        $row = $goodsTagService->findGoodsTag($id);

        View::engine()->layout(false);

        return View::fetch('',['row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();
        
        try{
            validate('GoodsTagValidate')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        $param['store_id'] = session('store_id');

        $model =  GoodsTag::create($param);

        return !empty($model['id']) ? json(['code' => 1, 'msg' => '保存成功!']) : json(['code' => 0, 'msg' => '保存失败!']);
        
    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            validate('GoodsTagValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        GoodsTag::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }

    public function update()
    {
        $param = $this->request->post();
        
        try{
            validate('GoodsTagValidate.edit')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        $model =  GoodsTag::update($param);

        return !empty($model['id']) ? json(['code' => 1, 'msg' => '更新成功!']) : json(['code' => 0, 'msg' => '更新失败!']);
    }


    public function delete()
    {
        $id = $this->request->get('id/d');

        $res = GoodsTag::destroy($id);

        return  $res ? json(['code' => 1,'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败']);

    }
}